package com.meirengu.webview.controller;

import com.meirengu.common.StatusCode;
import com.meirengu.model.Result;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    protected static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 全局异常捕捉处理
     *
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        logger.error(ExceptionUtils.getFullStackTrace(e));  // 记录错误信息
        Result result = new Result();
        result.setCode(StatusCode.INTERNAL_SERVER_ERROR);
        result.setData(null);
        result.setMsg(StatusCode.codeMsgMap.get(StatusCode.INTERNAL_SERVER_ERROR));
        return result;
    }
}
